package com.lancoo.answer.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;

import com.lancoo.answer.R;


/**
 * 仿猿题库：分屏上下结构
 */
public class SplitView extends ViewGroup implements View.OnTouchListener {

    private float mSplitRatio;
    private int mMinSplitTop;
    private int mMinSplitBottom;

    private View mTop, mMid, mBottom;
    private int mSplitHeight;
    private int mTouchSlop;
    private int mLastMotionY = 0;
    private int height, lastSplitHeight;
    private int oldSplitHeight = 0; //用于根据高度是否有变化来判断是否需要重绘


    public SplitView(Context context) {
        super(context);
        init(context, null);
    }

    public SplitView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        if (getChildCount() != 3) {
            throw new RuntimeException("give 3 views");
        }

        height = MeasureSpec.getSize(heightMeasureSpec);
        if (mSplitHeight == 0) {
            mSplitHeight = (int) (height * mSplitRatio);
        }

        //measure child
        measureChild(mTop, widthMeasureSpec, MeasureSpec.makeMeasureSpec(mSplitHeight, MeasureSpec.EXACTLY));
        measureChild(mMid, widthMeasureSpec, heightMeasureSpec);
        measureChild(mBottom, widthMeasureSpec, MeasureSpec.makeMeasureSpec(getMeasuredHeight() - mSplitHeight, MeasureSpec.EXACTLY));
    }

    private boolean isDrawed = false;

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        l = t = 0;
        if (!isDrawed) {   //topview在分屏滑动时不参与onlayout的过程，防止卡顿
            mTop.layout(l, t, l + mTop.getMeasuredWidth(), t + mTop.getMeasuredHeight());
            Log.e("aaaa", "topview参与绘制：");
            isDrawed = true;
        }

        mMid.layout(l, t + mSplitHeight - mMid.getMeasuredHeight(), l + mMid.getMeasuredWidth(), t + mSplitHeight);
        mBottom.layout(l, t + mSplitHeight, l + mBottom.getMeasuredWidth(), t + mSplitHeight + mBottom.getMeasuredHeight());
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        setupViews();
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                isDrawed = false;
                mLastMotionY = (int) event.getY();
                getParent().requestDisallowInterceptTouchEvent(false);
                return true;
            case MotionEvent.ACTION_MOVE:
                isDrawed = true;
                getParent().requestDisallowInterceptTouchEvent(true);   //内部拦截法：解决外层父布局横向和纵向滑动冲突的问题
                int delta = (int) (event.getY() - mLastMotionY);
                if (Math.abs(delta) > mTouchSlop) {
                    if (delta > 0) {
                        mSplitHeight += delta - mTouchSlop;
                    } else {
                        mSplitHeight += delta + mTouchSlop;
                    }

                    if (mSplitHeight < mMinSplitTop || mSplitHeight < mMid.getHeight()) {
                        mSplitHeight = Math.max(mMinSplitTop, mMid.getHeight());
                    } else if (mSplitHeight > getHeight() || mSplitHeight > getHeight() - mMinSplitBottom) {
                        mSplitHeight = Math.min(getHeight(), getHeight() - mMinSplitBottom);
                    }
                    requestLayout();
                }
                return true;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                isDrawed = false;
                requestLayout();
        }

        return false;
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ev_splitview);
        mSplitRatio = a.getFloat(R.styleable.ev_splitview_splitRatio, 0.382f);
        mMinSplitTop = a.getDimensionPixelSize(R.styleable.ev_splitview_minSplitTop, 0);
        mMinSplitBottom = a.getDimensionPixelSize(R.styleable.ev_splitview_minSplitBottom, 0);
        a.recycle();
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    protected void setupViews() {
        mTop = getChildAt(0);
        mMid = getChildAt(1);
        mBottom = getChildAt(2);
        View handlerView = mMid.findViewById(R.id.ib_center);
        if (handlerView == null) {
            handlerView = mMid;
        }
        handlerView.setOnTouchListener(this);
    }


    //设置初始化滑块位置
    public void setHandlerViewPosition(float ratio) {
        mSplitRatio = ratio;
        requestLayout();
    }

    //设置上方最小高度
    public void setMinSplitTop(int ratio) {
        mMinSplitTop = ratio;
    }

    //设置下方最小高度
    public void setmMinSplitBottom(int ratio) {
        mMinSplitBottom = ratio;
    }

    public void forceAdjsutAnswerHeight(float ratio) {
        int tempHeigh = (int) (height * ratio);
        lastSplitHeight = mSplitHeight;
        Log.e("eee","ratio："+ratio);
        Log.e("eee","tempHeigh："+tempHeigh);
        Log.e("eee","mSplitHeight："+mSplitHeight);
        if (tempHeigh > mSplitHeight) {
            return;
        }
        isDrawed=false;
        Log.e("eee","重新绘制高度了");
        mSplitHeight = (int) (height * ratio);
        isDrawed = false;
        requestLayout();
    }

    public void initLayout(float ratio){
        int tempHeigh = (int) (height * ratio);
        lastSplitHeight = mSplitHeight;
        Log.e("eee","ratio："+ratio);
        Log.e("eee","tempHeigh："+tempHeigh);
        Log.e("eee","mSplitHeight："+mSplitHeight);
        isDrawed=false;
        Log.e("eee","重新绘制高度了");
        mSplitHeight = (int) (height * ratio);
        isDrawed = false;
        requestLayout();
    }
}

